package com.deer.wms.inventory.service.impl;

//import com.deer.wms.base.system.dao.ContItemMapper;
//import com.deer.wms.base.system.model.container.ContItem;
//import com.deer.wms.base.system.model.container.ContainerInfo;
//import com.deer.wms.base.system.service.ContInfoService;
//import com.deer.wms.base.system.service.ContItemService;

import com.deer.wms.base.system.service.ItemInfoService;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.inventory.dao.StockProfitLossMapper;
import com.deer.wms.inventory.model.stockTake.StockProfitLoss;
import com.deer.wms.inventory.model.stockTake.StockProfitLossDto;
import com.deer.wms.inventory.model.stockTake.StockTakeLog;
import com.deer.wms.inventory.service.InventoryService;
import com.deer.wms.inventory.service.StockProfitLossService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.service.SuperServiceImpl;
import com.deer.wms.project.root.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by csl on 2021/04/13.
 */
@Service
@Transactional
public class StockProfitLossServiceImpl extends SuperServiceImpl<StockProfitLossMapper, StockProfitLoss> implements StockProfitLossService {

//    @Autowired
//    private ContItemService contItemService;
//
//    @Autowired
//    private ContItemMapper contItemMapper;
//    
//    @Autowired
//    private ContInfoService contInfoService;

    @Autowired
    private ItemInfoService itemInfoService;

    @Autowired
    private InventoryService inventoryService;

    @Autowired
    private StockProfitLossMapper stockProfitLossMapper;

    /**
     * 点击生成盈亏单自动生成盘盈入库单，盘亏出库单，并调整库存
     * @param stockProfitLossDto
     */
    @Override
    public void createProfitOrLoss(StockProfitLossDto stockProfitLossDto, CurrentUser currentUser) {
        //保存前先查询有没有保存过
        Integer num = stockProfitLossMapper.selectIsCreate(stockProfitLossDto.getStockMasterCode(),stockProfitLossDto.getWareId());
        if (num == 0){
            List<StockTakeLog> profitList = stockProfitLossDto.getProfitList();
            List<StockTakeLog> lossList = stockProfitLossDto.getLossList();
            //根据盘点差异信息调整容器库存
           // 盘盈入库 
            if (profitList.size()>0){
                for (StockTakeLog log : profitList){
                    //盘盈入库时需判断数据库是否有此库存，有就修改，没有就新增
                     // cont_item_id容器物料关联id
//                    if (log.getContItemId() != null){
//                        contItemService.updateQtyById(log.getContItemId(),log.getResidualQuantity());
//                    }else {//根据物料编码，容器编码，数量新增库存数据
//                        //根据物料编码查询物料id
//                        ItemInfo itemInfo = itemInfoService.findBy("itemCode",log.getItemCode());
//                        //根据容器编码查询容器id
//                        ContainerInfo containerInfo = contInfoService.findBy("contCode",log.getContCode());
//                        ContItem contItem = new ContItem();
//                        contItem.setItemId(itemInfo.getId());
//                        contItem.setContId(containerInfo.getId());
//                        contItem.setQty(log.getResidualQuantity());
//                        contItem.setNoLockQty(log.getResidualQuantity());
//                        contItem.setWareId(log.getWareId());
//                        contItem.setCreateDate(DateUtils.now("yyyy-HH-dd"));
//                        contItem.setProductDate(DateUtils.now("yyyy-HH-dd"));
//                        if (itemInfo.getIsPc() == 1){
//                            String nowTime = DateUtils.getNowDateTimeString();
//                            String buildNumber = nowTime.substring(0,10).replaceAll("-","")+"0001";
//                            contItem.setBuildNumber(buildNumber);
//                        }
//                        contItemService.save(contItem);
//                    }

                    //盘盈入库时需判断数据库是否有此库存，有就修改，没有就新增
                    //根据当前物料编码查询现有的库存表
                         //  更新库存
                    inventoryService.updateInOrOut(log.getWareId(),log.getItemCode(),log.getResidualQuantity(),"in");//调整总库存
                  
                }
            }
           // 盘盈出库
            if (lossList.size()>0){
                for (StockTakeLog log : lossList){
                    inventoryService.updateInOrOut(log.getWareId(),log.getItemCode(),log.getResidualQuantity()*-1,"out");//调整总库存
                }
            }
            //生成盘盈入库单
            if (profitList.size()>0){
                StockProfitLoss entity = new StockProfitLoss();
                entity.setStockMasterId(stockProfitLossDto.getStockMasterId());
                entity.setStockMasterCode(stockProfitLossDto.getStockMasterCode());
                entity.setCreateTime(DateUtils.getNowDateTimeString());
                entity.setWareId(stockProfitLossDto.getWareId());
                entity.setCreateUserId(currentUser.getUserId());
                entity.setCreateUserName(currentUser.getUserName());
                entity.setProfitOrLoss(1);
                entity.setProfitLossCode("Profit-"+stockProfitLossDto.getStockMasterCode());
                save(entity);
            }
            //生成盘亏出库单
            if (lossList.size()>0){
                StockProfitLoss entity = new StockProfitLoss();
                entity.setStockMasterId(stockProfitLossDto.getStockMasterId());
                entity.setStockMasterCode(stockProfitLossDto.getStockMasterCode());
                entity.setWareId(stockProfitLossDto.getWareId());
                entity.setCreateTime(DateUtils.getNowDateTimeString());
                entity.setCreateUserId(currentUser.getUserId());
                entity.setCreateUserName(currentUser.getUserName());
                entity.setProfitOrLoss(-1);
                entity.setProfitLossCode("Loss-"+stockProfitLossDto.getStockMasterCode());
                save(entity);
            }
        }else {
            throw new ServiceException(CommonCode.BATCH_TACTIC_ERROR, "已经生成过，请勿重复生成");
        }

    }

    /**
     * 查询盘盈和盘亏单
     * @param wareId
     * @param profitOrLoss
     * @return
     */
    @Override
    public List<StockProfitLoss> findByProfitLoss(Integer wareId, Integer profitOrLoss) {
        if (wareId==null){
            throw  new ServiceException(CommonCode.SYSTEM_ERROR,"仓库id不能为空！");
        }else if (profitOrLoss==null){
            throw  new ServiceException(CommonCode.SYSTEM_ERROR,"盘盈或盘亏状态不能为空！");
        }
        return stockProfitLossMapper.findByProfitLoss(wareId,profitOrLoss);
    }
}
